package com.cci.kangdao.service.impl;

import com.cci.kangdao.dao.WorkOrderSynchroTMapper;
import com.cci.kangdao.dao.WorkOrderTDao;
import com.cci.kangdao.dao.model.WorkOrderSynchroT;
import com.cci.kangdao.dao.model.WorkOrderT;
import com.cci.kangdao.service.WorkOrderSynchroService;
import com.cci.kangdao.utilTool.LocalTimeUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("WorkOrderSynchroService")
public class WorkOrderSynchroImpl implements WorkOrderSynchroService {

	private Logger log = Logger.getLogger(this.getClass().getName());
	
	@Resource
	private WorkOrderSynchroTMapper workOrderSynchroTMapper;
	@Resource
	private WorkOrderTDao workOrderTDao;

	@Override
	public boolean add(WorkOrderSynchroT record) {
		if(StringUtils.isNotBlank(record.getOrderNo())){
			WorkOrderT workOrderT = workOrderTDao.selectWorkOrderByOrderNo(record.getOrderNo());
		    if(null != workOrderT){
				record.setOrderId(workOrderT.getId());
				if(StringUtils.isBlank(record.getOrderNoCrm())){
					record.setOrderNoCrm(workOrderT.getOrderNoCrm());
				}
			}
		}
		record.setFlag(0);
		record.setCreater(-1L);
		record.setCreateTime(LocalTimeUtil.getRealCurrentTime());
		return workOrderSynchroTMapper.insert(record) > 0;
	}

	@Override
	public boolean updateById(WorkOrderSynchroT record) {
		if(StringUtils.isNotBlank(record.getOrderNo()) && (StringUtils.isBlank(record.getOrderNoCrm()) || null == record.getId())){
			WorkOrderT workOrderT = workOrderTDao.selectWorkOrderByOrderNo(record.getOrderNo());
			if(null != workOrderT){
				record.setOrderId(workOrderT.getId());
				record.setOrderNoCrm(workOrderT.getOrderNoCrm());
			}
		}
		record.setUpdater(-1L);
		record.setUpdateTime(LocalTimeUtil.getRealCurrentTime());
		return workOrderSynchroTMapper.updateById(record) > 0;
	}

	/**
	 * 根据工单编号和状态获取记录
	 * @param orderNo
	 * @return
	 */
	@Override
	public WorkOrderSynchroT selectByStatusAndOrderNo(String orderNo,Integer processStatus) {
		return workOrderSynchroTMapper.selectByStatusAndOrderNo(orderNo,processStatus);
	}

	/**
	 * 根据id获取记录
	 * @param id
	 * @return
	 */
	@Override
	public WorkOrderSynchroT selectById(Long id) {
		return workOrderSynchroTMapper.selectById(id);
	}

	/**
	 * 同步状态失败的记录
	 * @param channelName
	 * @return
	 */
	@Override
	public List<WorkOrderSynchroT> selectByStatus(String channelName) {
		return workOrderSynchroTMapper.selectByStatus(channelName);
	}

	/**
	 * 同步状态失败的分组记录
	 * @param channelName
	 * @return
	 */
	@Override
	public List<String> selectGroupOrderNoByStatus(String channelName) {
		return workOrderSynchroTMapper.selectGroupOrderNoByStatus(channelName);
	}

	/**
	 * 同步工单信息失败的记录
	 * @param channelName
	 * @return
	 */
	@Override
	public List<WorkOrderSynchroT> selectByInfo(String channelName) {
		return workOrderSynchroTMapper.selectByInfo(channelName);
	}

	/**
	 * 根据工单id查询同步工单状态失败的记录
	 * @param orderNo
	 * @return
	 */
	@Override
	public List<WorkOrderSynchroT> selectByOrderNo(String orderNo) {
		return workOrderSynchroTMapper.selectByOrderNo(orderNo);
	}
}
